/*
 * Project Name demo
 * File Name SocketRunner
 * Package Name com.huxiaosu.demo.socketio
 * Create Time 2023/1/28
 * Create by name：liujie
 */
package com.huxiaosu.demo.socketio;

import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

/**
 * Description
 *
 * @ClassName: SocketRunner
 * @author: liujie
 * @date: 2023/1/28 14:50
 */
@Slf4j
@Component
public class SocketRunner implements ApplicationRunner, ApplicationListener<ContextClosedEvent>, ApplicationContextAware {
    @Autowired
    private SocketIOServer socketIoServer;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        socketIoServer.start();
        log.info("socket start with {} : {}",socketIoServer.getConfiguration().getHostname(),socketIoServer.getConfiguration().getPort());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        if(socketIoServer!=null){
            socketIoServer.stop();
        }
    }
}